import axios from 'axios';

//const API_BASE_URL = 'http://101.42.2.4:8004/api';
const API_BASE_URL = 'http://117.72.106.234:8080/api';



// 创建axios实例
const authApi = axios.create({
  baseURL: API_BASE_URL,
  headers: {
    'Content-Type': 'application/json',
  },
});

// 请求拦截器：添加token
authApi.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('token');
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
authApi.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 认证相关API
export const authService = {
  // 发送验证码
  sendVerificationCode: (email, purpose) => {
    return authApi.post('/auth/send-verification-code', { email, purpose });
  },

  // 开放注册
  registerOpen: (data) => {
    return authApi.post('/auth/register/open', data);
  },

  // 邀请码注册
  register: (data) => {
    return authApi.post('/auth/register', data);
  },

  // 用户登录（支持用户名或邮箱登录）
  login: (account, password) => {
    return authApi.post('/auth/login', {
      account,  // 可以是用户名或邮箱
      password
    });
  },

  // 刷新Token
  refreshToken: () => {
    return authApi.post('/auth/refresh-token');
  },

  // 请求密码重置
  requestPasswordReset: (email) => {
    return authApi.post('/auth/request-password-reset', { email });
  },

  // 重置密码
  resetPassword: (data) => {
    return authApi.post('/auth/reset-password', data);
  },
}; 